package controllers;

import java.util.ArrayList;
import java.util.List;

import models.Email;
import models.Topic;
import models.User;
import play.jobs.Every;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

/**
 * The class TopicTriggerJob extends jobs and will be executed every 5 hours, to
 * check whether the topic has ideas or not..If not system will send triggers
 * (notifications and email) to the author to either delete the topic or add
 * ideas to it
 * 
 * @author Nourhan El-Nehlawy
 * 
 */

@Every("5h")
public class TopicTriggerJob extends play.jobs.Job {
	/**
	 * The method doJob() is executed every 5 hours to check if the topic is
	 * active or not If the topic is new, then it won't send triggers for the
	 * first time (notifications attribute is 30)
	 * 
	 * Then it'll keep sending triggres as long as it is inactive and
	 * notifications value is greater than 0, when notifications value is zero,
	 * system will delete the topic and notify the author
	 * 
	 * @author Nourhan El-Nehlawy
	 */
	public void doJob() throws EmailException {

		List<Topic> topicList = Topic.findAll();

		if (!topicList.isEmpty()) {
			for (int i = 0; i < topicList.size(); i++) {
				Topic topic = topicList.get(i);

				if (topic.ideas.isEmpty() && topic.notifications >= 0) {
					System.out.println(topicList.get(i).author.name);
					System.out.println(topicList.get(i).title);

					if (topic.notifications == 30) {
						topic.notifications--;
						topic.save();
						System.out.println(topic.notifications);
					} else if (topic.notifications != 0) {
						ArrayList<User> user = new ArrayList<User>();
						user.add(topic.author);
						topic.author.sendNotificationWithType(
								"Please add ideas to your topic or delete it",
								user, "Topic", topic.id);

						String source = "topsoft.system@gmail.com";
						String destination = topic.author.email;
						Email email = new Email(source, destination,
								"Adding ideas To inactive Topic",
								"Please add ideas to your topic " + topic.title
										+ " or delete it");
						email.sendEmail();
						topic.notifications--;
						topic.save();
						System.out.println(topic.notifications);

					} else if (topic.notifications == 0) {
						System.out.println("Your Topic will be deleted");

						ArrayList<User> user = new ArrayList<User>();
						user.add(topic.author);
						topic.author.sendNotificationWithType(
								"Please note that your topic MUST be deleted",
								user, "Topic", topic.id);

						String source = "topsoft.system@gmail.com";
						String destination = topic.author.email;
						Email e = new Email(source, destination,
								"Deleting your Topic",
								"Please note that your topic " + topic.title
										+ " will be deleted");
						e.sendEmail();

						topic.notifications--;
						topic.save();

					}
				} else if (!topic.ideas.isEmpty()) {
					topic.notifications = 30;
					topic.save();
				}

			}
		}
	}
}
